from typing import Any, Annotated
from datetime import timedelta,datetime
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
from fastapi import APIRouter, Depends, HTTPException, status, Path
from sqlalchemy.orm.session import Session
from common.deps import check_jwt_token, get_db
from core.response import response_code
from datetime import datetime
from setting.web import settings
from core.schemas.classroom import ClassroomCreate, ClassroomUpdate ,DateTimeArray ,ClassroomMapper
import crud

router = APIRouter()

@router.get("/getMap", summary="得到地图")
async def get_mapper(db: Session = Depends(get_db),payload=Depends(check_jwt_token)):
    res = crud.classroom.get_mapper(db)
    return response_code.resp_200(data=res)

@router.get("/list", summary="列出教室")
async def list_classrooms(page: int = 1, pageSize: int = 20,  db: Session = Depends(get_db),payload=Depends(check_jwt_token)):
    res = crud.classroom.get_classrooms(db,page,pageSize)
    return response_code.resp_200(data=res)

@router.post("/listAvailable", summary="列出时间段内可用教室")
async def list_available_classrooms(page: int = 1, pageSize: int = 20, timeSpan: DateTimeArray = {'reservationTime':[datetime.now().strftime("%Y-%m-%d %H:%M:%S"),datetime.now().strftime("%Y-%m-%d %H:%M:%S")]}, db: Session = Depends(get_db),payload=Depends(check_jwt_token)):
    res = crud.classroom.get_available_classrooms(db,page,pageSize,timeSpan.get_times_as_datetimes()[0],timeSpan.get_times_as_datetimes()[1])
    return response_code.resp_200(data=res)

@router.get("/{id}", summary="获取教室信息")
async def get_classroom(id: Annotated[int, Path(title="教室id编号")], db: Session = Depends(get_db),payload=Depends(check_jwt_token)):
    res = crud.classroom.get_classroom_by_id(db,id)
    return response_code.resp_200(data=res)

@router.post("/mapper", summary="可视化映射")
async def classroom_mapper(new_data: ClassroomMapper, db: Session = Depends(get_db),payload=Depends(check_jwt_token)):
    res = crud.classroom.bind_building(db,new_data.building_name,new_data.object_name)
    return response_code.resp_200(data=res)

